home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / CH_5.5 / FITLINE / mbtoendpts.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  2.5 KB  |  85 lines

  1. /* MBTOENDPTS:  function converts a parameterized line consisting of
  2.  *            slope (m), y-intercept (b), and two endpoints of data
  3.  *              into a line fit by the parameters, but bounded by the
  4.  *              bounding box of the endpoints 
  5.  *                      usage: mbtoendpts (end1, end2, m, b, width, height,
  6.  */
  7.  
  8. #define ABS(A) (((A) >= 0.0) ? (A): -(A))  /* integer absolute value */
  9.  
  10. #include <images.h>
  11.  
  12. int
  13. mbtoendpts (end1, end2, m, b, width, height, pt1, pt2)
  14.      struct point end1, end2;   /* endpoints of data */
  15.      double m, b;               /* slope and y-intercept parameters of fit */
  16.      long width, height;        /* image size */
  17.      struct point *pt1, *pt2;   /* endpoints of line fit */
  18.  
  19. {
  20.   struct dpoint min, max;       /* floating point point */
  21.  
  22. /* determine bounding box */
  23.   min.x = (end1.x < end2.x) ? (double) end1.x : (double) end2.x;
  24.   min.y = (end1.y < end2.y) ? (double) end1.y : (double) end2.y;
  25.   max.x = (end1.x > end2.x) ? (double) end1.x : (double) end2.x;
  26.   max.y = (end1.y > end2.y) ? (double) end1.y : (double) end2.y;
  27.  
  28. /* endpoints for zero slope */
  29.   if (m == 0.0) {
  30.     pt1->x = (long) (min.x + 0.5);
  31.     pt1->y = (long) (min.y + 0.5);
  32.     pt2->x = (long) (max.x + 0.5);
  33.     pt2->y = (long) (max.y + 0.5);
  34.     return (0);
  35.   }
  36.  
  37. /* endpoints are upper and lower bounds of bounding box, depending on slope */
  38.   if (ABS (m) < 1.0) {          /* use x bounds for low slope */
  39.     pt1->x = (long) min.x;
  40.     pt1->y = (long) (m * min.x + b + 0.5);
  41.     if (pt1->y < 0) {
  42.       pt1->y = 0;
  43.       pt1->x = (long) ((-b) / m + 0.5);
  44.     }
  45.     else if (pt1->y >= height) {
  46.       pt1->y = height - 1;
  47.       pt1->x = (long) ((pt1->y - b) / m + 0.5);
  48.     }
  49.     pt2->x = (long) max.x;
  50.     pt2->y = (long) (m * max.x + b + 0.5);
  51.     if (pt2->y < 0) {
  52.       pt2->y = 0;
  53.       pt2->x = (long) ((-b) / m + 0.5);
  54.     }
  55.     else if (pt2->y >= height) {
  56.       pt2->y = height - 1;
  57.       pt2->x = (long) ((pt2->y - b) / m + 0.5);
  58.     }
  59.   }
  60.   else {
  61.     pt1->y = (long) min.y;
  62.     pt1->x = (long) ((min.y - b) / m + 0.5);
  63.     if (pt1->x < 0) {
  64.       pt1->x = 0;
  65.       pt1->y = (long) (b + 0.5);
  66.     }
  67.     else if (pt1->x >= width) {
  68.       pt1->x = width - 1;
  69.       pt1->y = (long) (m * pt1->x + b + 0.5);
  70.     }
  71.     pt2->y = (long) max.y;
  72.     pt2->x = (long) ((max.y - b) / m + 0.5);
  73.     if (pt2->x < 0) {
  74.       pt2->x = 0;
  75.       pt2->y = (long) (b + 0.5);
  76.     }
  77.     else if (pt2->x >= width) {
  78.       pt2->x = width - 1;
  79.       pt2->y = (long) (m * pt2->x + b + 0.5);
  80.     }
  81.   }
  82.  
  83.   return (0);
  84. }
  85.